package com.wangsd.web.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangsd.web.coupon.entity.ServiceBusiness;
import com.wangsd.web.coupon.mapper.ServiceBusinessMapper;
import com.wangsd.web.coupon.service.ServiceBusinessService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import javax.xml.ws.Action;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author generator
 * @since 2019-09-18
 */
@Service
public class ServiceBusinessServiceImpl extends ServiceImpl<ServiceBusinessMapper, ServiceBusiness> implements ServiceBusinessService {

    @Resource
    ServiceBusinessMapper serviceBusinessMapper;

    @Override
    @Transactional
    public Integer addOrUpdate(ServiceBusiness serviceBusiness) {
        if (serviceBusiness.getId() != null) {
            serviceBusinessMapper.updateById(serviceBusiness);
        } else {
            serviceBusinessMapper.insert(serviceBusiness);

            //保存检索CODE
            serviceBusiness.setCode(serviceBusiness.getId() + "");
            serviceBusinessMapper.updateById(serviceBusiness);
        }
        return 1;
    }

    @Override
    public List<ServiceBusiness> selectAllByUserCode(String code) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("id", "name", "code");
        if (StringUtils.isNotBlank(code)) {
            queryWrapper.likeRight("code", code);
        }
        List<ServiceBusiness> list = serviceBusinessMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public IPage<ServiceBusiness> selectPage(String code, Integer pageNum, Integer pageSize, ServiceBusiness serviceBusiness) {
        Page<ServiceBusiness> page = new Page<>(pageNum, pageSize);
        QueryWrapper<ServiceBusiness> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(code)){
            queryWrapper.likeRight("code",code);
        }
        if(StringUtils.isNotBlank(serviceBusiness.getName())){
            queryWrapper.like("name",serviceBusiness.getName());
        }
        IPage<ServiceBusiness> serviceBusinessIPage = serviceBusinessMapper.selectPage(page, queryWrapper);
        return serviceBusinessIPage;
    }

    @Override
    public ServiceBusiness selectBusinessByCode(String code) {
        String[] ids = code.split("-");
        if (ids != null && ids.length >= 2) {
            Long businessId = Long.valueOf(ids[0]);
            return serviceBusinessMapper.selectById(businessId);
        }
        return null;
    }

    @Override
    public List<ServiceBusiness> selectByCode(String code) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(code)) {
            queryWrapper.eq("code", code);
        }
        queryWrapper.eq("deleted", "0");
        List<ServiceBusiness> list = serviceBusinessMapper.selectList(queryWrapper);
        return list;
    }
}
